package GUI;

import javax.swing.JPanel;

import Entity.CasualSubscription;
import Entity.FullSubscription;
import Entity.Subscription;
import javax.swing.BoxLayout;
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import Controller.Client;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;

/**
 * this is the GUI for information station subscription. the customer will enter his subscription nummber and car number. 
 * @author Or Marx
 *
 */
public class InformationStationSubscriptionGui extends JPanel{
	private JTextField textField;
	private JTextField textField_1;
	JFrame frame;
	JPanel lastPanel;
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}
	/**
	 * is the constructor. it has only one button that will validate the information typed by the customer.
	 */
	public InformationStationSubscriptionGui() {
		this.setLayout(null);
		
		JLabel lblSubscriptionNumber = new JLabel("Subscription Number");
		lblSubscriptionNumber.setBounds(283, 116, 141, 28);
		this.add(lblSubscriptionNumber);
		
		textField = new JTextField();
		textField.setBounds(434, 120, 154, 20);
		this.add(textField);
		textField.setColumns(10);
		
		JLabel lblCarNumber = new JLabel("Car number");
		lblCarNumber.setBounds(283, 163, 121, 20);
		this.add(lblCarNumber);
		
		textField_1 = new JTextField();
		textField_1.setBounds(434, 163, 154, 20);
		this.add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnConfirm = new JButton("confirm");
		btnConfirm.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent arg0) {
				CasualSubscription cSub=Client.getCasualSubscriptionByNumber(Integer.valueOf(textField.getText()));
				FullSubscription fSub=Client.getFullSubscriptionByNumber(Integer.valueOf(textField.getText()));
				if (fSub!=null)	//if its a full subscription
				{
					if (fSub.getCar_number().compareTo(textField_1.getText())==0)	//double check with car number
					{
						Date now=new Date();
						if (now.compareTo(fSub.getEnd_date()) < 0)	//check the end date of the subscription
						{
							//insert the car in "if" so it return false if the parking lot is full
							if(Client.getParkingLots_sorted()[Client.getParkingLot()-1].insertCar(fSub.getCar_number(), new Date(now.getTime()+1000*60*60*24*14)))
								{
									JOptionPane.showMessageDialog(null,"enjoy your parking!","full Subscription",JOptionPane.INFORMATION_MESSAGE);
									frame.setContentPane((JPanel)lastPanel);
								}
							else
								JOptionPane.showMessageDialog(null,"no parking spots available! please go away.","Subscription",JOptionPane.ERROR_MESSAGE);
						} 
						else
							JOptionPane.showMessageDialog(null,"the subscription is out of date","Subscription",JOptionPane.ERROR_MESSAGE);
							
					}
					else
						JOptionPane.showMessageDialog(null,"wrong car number" ,"Subscription",JOptionPane.ERROR_MESSAGE);
						
				}
				else if (cSub!=null)
				{
					if (fSub.getCar_number().compareTo(textField_1.getText())==0)
					{
						Date now=new Date();
						if (now.compareTo(cSub.getEnd_date()) < 0)
						{
							if (Client.getParkingLot()==cSub.getParking_lot_number())	//check if this is the parking lot of the casual subscription
							{
								if ((new Date()).getDay()!=6)
								{
								Date endParking=new Date();
								endParking.setHours(cSub.getleaving_time().getHours());
								endParking.setMinutes(cSub.getleaving_time().getMinutes());
								if (endParking.compareTo(now)<0)	//calculate expected leaving hour: if the hour is passed, the expected leaving hour is tomorrow.
									endParking.setTime(endParking.getTime()+1000*60*60*24);
								if(Client.getParkingLots_sorted()[Client.getParkingLot()-1].insertCar(cSub.getCar_number(), endParking))
									{
										JOptionPane.showMessageDialog(null,"enjoy your parking!","casual Subscription",JOptionPane.INFORMATION_MESSAGE);
										frame.setContentPane((JPanel)lastPanel);
									}
								else
									JOptionPane.showMessageDialog(null,"no parking spots available! please go away.","Subscription",JOptionPane.ERROR_MESSAGE);
								}
								else
									JOptionPane.showMessageDialog(null,"casual subscriber can only enter during the week (now is weekend)","Subscription",JOptionPane.ERROR_MESSAGE);

							}
							else
								JOptionPane.showMessageDialog(null,"the subscription isnt good for this parking lot","Subscription",JOptionPane.ERROR_MESSAGE);
						}
						else
							JOptionPane.showMessageDialog(null,"the subscription is out of date","Subscription",JOptionPane.ERROR_MESSAGE);
					}
					else
						JOptionPane.showMessageDialog(null,"wrong car number","Subscription",JOptionPane.ERROR_MESSAGE);
				}
				else
					JOptionPane.showMessageDialog(null,"wrong subscription number","Subscription",JOptionPane.ERROR_MESSAGE);
				
			}
		});
		btnConfirm.setBounds(283, 314, 89, 23);
		this.add(btnConfirm);
		
		JLabel lblEnterDetailsTo = new JLabel("enter details to identify yourself as a subscriber");
		lblEnterDetailsTo.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblEnterDetailsTo.setBackground(Color.WHITE);
		lblEnterDetailsTo.setBounds(283, 11, 384, 43);
		this.add(lblEnterDetailsTo);
		
		JButton btnCancel = new JButton("cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setContentPane((JPanel)lastPanel);
			}
		});
		btnCancel.setBounds(552, 314, 89, 23);
		this.add(btnCancel);
	}
}
